package logic

import (
	"context"
	"doc-manage/model/models"
	"doc-manage/model/mysql"
	"gorm.io/gorm"

	"doc-manage/docrpc/docrpc"
	"doc-manage/docrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DocUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDocUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DocUpdateLogic {
	return &DocUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DocUpdateLogic) DocUpdate(in *docrpc.DocUpdateRequest) (*docrpc.DocUpdateResponse, error) {
	// todo: add your logic here and delete this line

	doc := models.Doc{
		Model:      gorm.Model{ID: uint(in.DocId)},
		DocName:    in.DocName,
		DocType:    in.DocType,
		DocContent: in.DocContent,
	}
	err := doc.DocUpdate(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &docrpc.DocUpdateResponse{
		DocId: int64(doc.ID),
	}, nil
}
